package com.sjht.android.bxgz.activity;

import android.app.Activity;
import android.app.ActivityGroup;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.DisplayMetrics;
import android.view.KeyEvent;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.ViewFlipper;

import com.sjht.android.bxgz.BxgzApp;
import com.sjht.android.bxgz.R;
import com.sjht.android.bxgz.utils.Utils;

@SuppressWarnings("deprecation")
public class MainTabActivity extends ActivityGroup implements OnCheckedChangeListener {

	private BxgzApp mApp;
	private Context mContext;
	private RadioGroup rg_tab;
	private RadioButton[] rb_tabs;
	private Class<?>[] clazzs;
	private boolean isExit = false;
	private int oldSwitchId = 0;
	

	
	private ViewFlipper vf_container;


	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		mContext = this;
		mApp = BxgzApp.getSelf();
		setContentView(R.layout.maintab);

		initView();
		rb_tabs[oldSwitchId].setChecked(true);
	}

	private void initView() {
		vf_container = (ViewFlipper) findViewById(R.id.vf_container);
		
		rg_tab = (RadioGroup) findViewById(R.id.rg_tab);

		rb_tabs = new RadioButton[4];
		clazzs = new Class<?>[4];
		for (int i = 0; i < 4; i++) {
			rb_tabs[i] = (RadioButton) rg_tab.findViewWithTag("rb_tab" + i);
			rb_tabs[i].setOnCheckedChangeListener(this);
//			switch (i) {
//			case 0:
//				clazzs[i] = HomeActivity.class;
//				break;
//
//			case 1:
//				clazzs[i] = SearchActivity.class;
//				break;
//				
//			case 2:
//				clazzs[i] = MyBellsActivity.class;
//				break;
//				
//			case 3:
//				clazzs[i] = MoreActivity.class;
//				break;
//			default:
//				break;
//			}
			
		}
	}
	

	@Override
	public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
		// TODO Auto-generated method stub
		if (isChecked) {
			for (int i = 0; i < rb_tabs.length; i++) {
				if (buttonView == rb_tabs[i]) {
					vf_container.removeAllViews();// 必须先清除容器中扄1�7有的View
					DisplayMetrics dm = new DisplayMetrics();
					getWindowManager().getDefaultDisplay().getMetrics(dm);
					Window window = null;
//					switchActivity(window, clazzs[i], rb_tabs[i].getTag().toString());
					oldSwitchId = i;
				} 
			}
		}
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_SEARCH) {
			return true;
		} else if (keyCode == KeyEvent.KEYCODE_BACK) {
//			if (event.getRepeatCount() == 0) {
//				if (oldSwitchId == 0) {
//					if(isExit) {
//						finish();
//						mApp.exit();
//					} else {
//						Utils.toast(mContext, R.string.exit_hint);
//						isExit = true;
//						new Handler().postDelayed(new Runnable() {
//							
//							@Override
//							public void run() {
//								// TODO Auto-generated method stub
//								isExit = false;
//							}
//						}, 1000);
//					}
//					
//				} else {
//					rb_tabs[0].setChecked(true);
//				}
//			}
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}
	
	private void switchActivity(Window window, Class<?> clazz, String id) {
		switchActivity(window, clazz, id, null);
	}
	
	private void switchActivity(Window window, Class<?> clazz, String id, Bundle bundle) {

		// Activity 转为 View
		Activity activity = getLocalActivityManager().getActivity(id);
		if (activity != null) {
			Intent intent = activity.getIntent();
			if (bundle != null)
				intent.putExtras(bundle);
			window = getLocalActivityManager().startActivity(id, intent);
		} else {
			Intent intent = new Intent(this, clazz);
			if (bundle != null)
				intent.putExtras(bundle);
			window = getLocalActivityManager().startActivity(id, intent);
		}

		// 容器添加View
		vf_container.addView(window.getDecorView(), LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
		vf_container.showNext();
	}
}
